[% setvar title Allow grouping of -X file tests and add C<filetest> builtin %]
<div id="archive-notice">
    <h3>This file is part of the Perl 6 Archive</h3>
    <p>To see what is currently happening visit <a href="http://www.perl6.org/">http://www.perl6.org/</a></p>
</div>
<div class='pod'>
<a name='TITLE'></a><h1>TITLE</h1>
<p>Allow grouping of -X file tests and add <code>filetest</code> builtin</p>
<a name='VERSION'></a><h1>VERSION</h1>
<pre>  Maintainer: Nathan Wiger &lt;<a href='mailto:nate@wiger.org'>nate@wiger.org</a>&gt;
  Date: 25 Sep 2000
  Last Modified: 30 Sep 2000
  Mailing List: <a href='mailto:perl6-language@perl.org'>perl6-language@perl.org</a>
  Number: 320
  Version: 2
  Status: Frozen</pre>
<a name='ABSTRACT'></a><h1>ABSTRACT</h1>
<p>Currently, file tests cannot be grouped, resulting in very long
expressions when one wants to check to make sure some thing is a
readable, writeable, executable directory:</p>
<pre>   if ( -d $file &amp;&amp; -r $file &amp;&amp; -w $file &amp;&amp; -x $file ) { ... }</pre>
<p>It would be really nice if these could be grouped instead:</p>
<pre>   if ( -drwx $file ) { ... }</pre>
<p>Notice how much easier this is to read and write.</p>
<a name='NOTES ON FREEZE'></a><h1>NOTES ON FREEZE</h1>
<p>Everyone liked it (I even heard Tom say so :), the only concern raised
was that it makes the direct negation of subs impossible because of the
way <code>-[a-zA-Z]+</code> would have to be tokenized. See the MIGRATION and also
REFERENCES sections for details on this.</p>
<a name='DESCRIPTION'></a><h1>DESCRIPTION</h1>
<a name='File Test Grouping'></a><h2>File Test Grouping</h2>
<p>See above. Multiple file tests, when grouped, should be ANDed together.
This RFC does not propose a way to OR them, since usage like this:</p>
<pre>   if ( -d $file || -r $file || -w $file || -x $file ) { ... }</pre>
<p>Is highly uncommon, to say the least.</p>
<p>Notice this has the nice side effect of eliminating the need for <code>_</code> in
many cases, since this:</p>
<pre>   if ( -d $file &amp;&amp; -r _ &amp;&amp; -w _ &amp;&amp; -x _ ) { ... }</pre>
<p>Can simply be written as a single grouped file test, as shown above. If
you need to check for more complex logic, you still have to do that
separately:</p>
<pre>   if ( -drwx $file and ! -h $file )  { ... }</pre>
<p>This is the simplest and also probably the clearest way to implement
this.</p>
<a name='New filetest Builtin'></a><h2>New <code>filetest</code> Builtin</h2>
<p>This RFC also proposes a new <code>filetest</code> builtin that is actually what
is used for these tests. The <code>-[a-zA-Z]+</code> form is simply a shortcut to
this builtin, just like &lt;&gt; is a shortcut to <code>readline</code>. So:</p>
<pre>   if ( -rwdx $file ) { ... }</pre>
<p>Is really just a shortcut to the <code>filetest</code> builtin:</p>
<pre>   if ( filetest $file, 'rwdx' ) { ... }</pre>
<p>Either form could be used, depending on the user's preferences (just
like <code>readline</code>). Note that this <code>filetest</code> builtin is designed to
supposedly make the implementation of this easier, but if it doesn't,
then it's unnecessary and should not be added.</p>
<a name='IMPLEMENTATION'></a><h1>IMPLEMENTATION</h1>
<p>This would involve making <code>-[a-zA-Z]+</code> a special token in all contexts,
serving as a shortcut for the <code>filetest</code> builtin. This means that you
will no longer be able to directly negate subroutine calls; see below.</p>
<a name='MIGRATION'></a><h1>MIGRATION</h1>
<p>There is a subtle trap if you are negating subroutines:</p>
<pre>   $result = -drwx $file;</pre>
<p>And expect this to be parsed like this:</p>
<pre>   $result = - &amp;drwx($file);</pre>
<p>However, usage such as this is not common, since negating subs only
makes sense in a very few cases. In Perl 6, instead of writing:</p>
<pre>   $num = -exp($foo);</pre>
<p>You would have to write:</p>
<pre>   $num = - exp($foo);</pre>
<p>The author personally feels this is not too much of a burden for the
benefit of grouped filetests. Note that this is already required for
people that use subs named r, w, d, or any other filetest character.</p>
<p>To fix this issue, the p52p6 translator simply has to look for
<code>-([a-zA-Z]{2,})</code> and replace it with <code>- $1</code>, since injecting a
single space will break up the token. See the below links for more
details on the discussions of this.</p>
<a name='REFERENCES'></a><h1>REFERENCES</h1>
<p><a href='http://www.mail-archive.com/perl6-language@perl.org/msg04649.html' target='_blank'>www.mail-archive.com</a></p>
<p><a href='http://www.mail-archive.com/perl6-language@perl.org/msg04658.html' target='_blank'>www.mail-archive.com</a></p>
</div>
